package gz.copydatabase.Utils;

import android.os.Environment;
import android.util.Log;
import android.util.Xml;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import org.xmlpull.v1.XmlSerializer;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import gz.copydatabase.Bean.Config;


/**
 * Created by Administrator on 2017/11/30.
 */

public class SaxUtils {
    public static Config readXML() {
        try {
           /// File file= new File(Environment.getExternalStorageDirectory(), "/Park/Config/config.txt");
            File[] files=FileUtils.getConfigFileNames();
            FileInputStream fis = new FileInputStream(files[0]);
            InputSource is = new InputSource(fis);
            //实例化SAX工厂类
            SAXParserFactory factory= SAXParserFactory.newInstance();
            //实例化SAX解析器。
            SAXParser sParser=factory.newSAXParser();
            //实例化DefaultHandler，设置需要解析的节点
            SaxHandler handler=new SaxUtils.SaxHandler();
            // 开始解析
            sParser.parse(is,handler);
            // 解析完成之后，关闭流
            fis.close();
            //返回解析结果。
            return handler.result();
        } catch (Exception e) {

        }
        return null;
    }
    public static void  saveXML() {
        try {
            File path= new File(Environment.getExternalStorageDirectory() + "/BankBox/Config");
            if (!path.exists() && !path.isDirectory()) {
                path.mkdirs();
               // ShellUtils.execCommand("chmod -R 666 "+"/storage/sdcard/Park/",true);
            }
            File file= new File(path.getPath(),"config.xml");
            File[] files=FileUtils.getConfigFileNames();
            if (files.length==0&&!file.exists()) {
                file.createNewFile();
                FileOutputStream fos = new FileOutputStream(file);
                // 获得一个序列化工具
                XmlSerializer serializer = Xml.newSerializer();
                serializer.setOutput(fos, "utf-8");
                // 设置文件头
                String enter ="\r\n";//System.getProperty("line.separator");//换行
                serializer.startDocument("utf-8", true);
                serializer.text(enter);
                serializer.startTag(null, "config");
                serializer.text(enter);

                serializer.comment("reboot: 0表示定时重启 1表示不重启");
                serializer.text(enter);
                serializer.startTag(null, "reboot");
                serializer.text("1");
                serializer.endTag(null, "reboot");
                serializer.text(enter);

                serializer.comment("led: 1表示仰邦BX5K1 2表示方控");
                serializer.text(enter);
                serializer.startTag(null, "led");
                serializer.text("2");
                serializer.endTag(null, "led");
                serializer.text(enter);


                serializer.startTag(null, "display");
                serializer.text(enter);
                serializer.comment("lineedit1:太航：禁止通行 其他：欢迎光临");
                serializer.text(enter);
                serializer.startTag(null, "lineedit1");
                serializer.text("欢迎光临");
                serializer.endTag(null, "lineedit1");
                serializer.text(enter);
                serializer.comment("lineedit2:太航：临时车 禁止通行 其他：临时车");
                serializer.text(enter);
                serializer.startTag(null, "lineedit2");
                serializer.text("临时车");
                serializer.endTag(null, "lineedit2");
                serializer.text(enter);
                serializer.endTag(null, "display");
                serializer.text(enter);
                serializer.endTag(null, "config");
                serializer.endDocument();
                fos.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    static class  SaxHandler extends DefaultHandler {
        private Config  config;
        private String content;
        public Config result(){
            return  config;
        }
        public SaxHandler(){
        }
        @Override
        public void startDocument() throws SAXException {
            super.startDocument();
        }
        @Override
        public void endDocument() throws SAXException {
            super.endDocument();
        }
        @Override
        public void characters(char[] ch, int start, int length)
                throws SAXException {
            super.characters(ch, start, length);
            content = new String(ch, start, length);
        }
        @Override
        public void startElement(String uri, String localName, String qName,
                                 Attributes attributes) throws SAXException {
            super.startElement(uri, localName, qName, attributes);

            if("config".equals(localName)){
                config = new Config();
            }
            if("display".equals(localName)){
               // display=new Config.Display();
                config.setDisplay(new Config.Display());
            }
        }
        @Override
        public void endElement(String uri, String localName, String qName)
                throws SAXException {
            super.endElement(uri, localName, qName);
            if("reboot".equals(localName)){
                Log.i("hello123",content);
                config.setReboot(content.equals("0")?true:false);
            }
            if("led".equals(localName)){
                Log.i("hello123",content);
                config.setLed(Integer.valueOf(content));
            }
            if("lineedit1".equals(localName)){
                config.getDisplay().setLineEdit1(content);
            }
            if("lineedit2".equals(localName)){
                config.getDisplay().setLineEdit2(content);
            }
        }
    }
}
























